<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class UserNo extends Node
{

    protected $name;
    protected $first_surname;
    protected $second_surname;
    protected $fiscal_id;
    protected $username;
    protected $password; //md5 representation of the password
    protected $mail;
    protected $avatar;
    protected $full_name;
    protected $asigned = false;
    protected $desasignable = false;
    protected $editable = false;

    function doPOST( Event $e )
    {
        if ( $e->tag == 'asign' ) {
            $this->asignar();
        }
    }

    function asignar()
    {
        
    }

    /**
     * Comprueba que el NIE dado sea correcto
     * @param string nie NIE
     * @return boolean
     */
    static function control_nie( $nie )
    {
        $nie = strtoupper( $nie );
        if ( strlen( $nie ) < 9 ) return false;

        switch ( $nie[ 0 ] ) {
            case 'L':
                return self::control_nif( '0' . substr( $nie, 1 ) );
            case 'M':
                return self::control_nif( '0' . substr( $nie, 1 ) );
            case 'X':
                return self::control_nif( '0' . substr( $nie, 1 ) );
            case 'Y':
                return self::control_nif( '1' . substr( $nie, 1 ) );
            case 'Z':
                return self::control_nif( '2' . substr( $nie, 1 ) );
        }
        return false;
    }

    static function control_nif( $dni )
    {
        $dni = ( string ) ($dni);

        if ( preg_match( '/^\d{8}[TRWAGMYFPDXBNJZSQVHLCKE]$/', $dni ) )
                return ($dni[ 8 ] == self::letra_nif( $dni ));

        return false;
    }

    static function letra_nif( $nif )
    {
        if ( preg_match( '/^\d{8}[TRWAGMYFPDXBNJZSQVHLCKE]/', $nif ) ) {
            $cadena = 'TRWAGMYFPDXBNJZSQVHLCKE';

            return $cadena[ (( int ) substr( $nif, 0, 8 )) % 23 ];
        }
        else return '';
    }

}

?>
